<?php

namespace App\Models\Home;

use Illuminate\Database\Eloquent\Model;
use App\Models\Home\DeliverSchedule;

class Position extends Model
{
    //
    protected $table = 'position';
    protected $guarded = [];

    public function user()
    {
        return $this->belongsTo('App\Models\Home\Users','uid','id');
    }

    public function assign_user()
    {
        return $this->belongsTo('App\Models\Home\Users','assign','id');
    }

    public function client()
    {
        return $this->belongsTo('App\Models\Home\Client','client_id','id');
    }

    public function resume()
    {
        return $this->belongsToMany('App\Models\Home\Resume','deliver','position_id','resume_id');
    }

    public function position_class()
    {
        return $this->belongsTo('App\Models\Home\PositionClass','category','id');
    }

    public function deliver()
    {
        return $this->hasMany('App\Models\Home\Deliver', 'position_id', 'id');
    }

    public function getList($skip,$limit,$opt=array(),$in=array(),$user=true,$assign=true,$client=true,$all=false,$doesnt=false){

        if (!isset($limit)||!isset($skip)){
            return [];
        }
        if (!isset($skip)){
            $skip = 0;
        }

        $query = Position::orderBy('id', 'desc');
        
        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){

            $query = $query->whereIn($in[0],$in[1]);
        }

        if (!$all){
            $query = $query->skip($skip)->limit($limit);
        }

        if ($user){
            $query = $query->with(['user']);
        }

        if ($assign){
            $query = $query->with(['assign_user.frame.belong_user']);
        }

        if ($client){
            $query = $query->with(['client']);
        }

        if ($doesnt){
            $query->whereDoesntHave('resume', function ($query) use($doesnt) {
                $query->where('resume.id', $doesnt)->where('deliver.status',1);
            });
        }

        $list = $query->with(['position_class'])->get();
   
        foreach ($list as $k => $v){

            $list[$k]->cv = $this->getCV($v);

            if ($user){
                $list[$k]->username = $v->user?$v->user->username:'';
            }

            if ($assign){
                $list[$k]->assign_username = $v->assign_user?$v->assign_user->username:'';
                $list[$k]->team_username = $v->assign_user?$v->assign_user->frame?$v->assign_user->frame->belong_user?$v->assign_user->frame->belong_user->username:'':'':'';
            }

            if ($client){
                $list[$k]->client_name = $v->client?$v->client->chinese_name:'';
                $replace_name = $v->client?$v->client->replace_name:'';
                if ($replace_name){
                    $list[$k]->replace_name = $replace_name;
                }else{
                    $list[$k]->replace_name = mb_substr($list[$k]->client_name,0,2);
                }
            }

            switch ($v->emergency){
                case 1:
                    $list[$k]->emergency_name = '紧急重要，优质客户';
                    break;
                case 2:
                    $list[$k]->emergency_name = '紧急不重要，一般客户';
                    break;
                case 3:
                    $list[$k]->emergency_name = '不紧急不重要，新客户';
                    break;
                default:
                    $list[$k]->emergency_name = '';
            }

//            $list[$k]->category_name = $v->position_class?$v->position_class->name:'';//删除
            $list[$k]->region_name = $v->region?config('base.country.'.$v->region.'.name'):'';
            $list[$k]->range_name = $v->range?config('base.working_salary.'.$v->range.'.name'):'';
            switch ($v->state){
                case 0:
                    $list[$k]->state_name = 'closed';
                    break;
                case 1:
                    $list[$k]->state_name = '招聘中';
                    break;
                default:
                    $list[$k]->state_name = 'closed';
            }
        }

        return $list;
    }

    public function getCount($opt=array(),$in=array()){

        $query = Position::orderBy('id', 'desc');

        if (!empty($opt)){
            foreach ($opt as $k => $v){
                if(is_array($v) && $v[1]){
                    $query = $query->where($k,$v[0],$v[1]);
                }elseif(!is_array($v) && $v !== false){
                    $query = $query->where($k,$v);
                }
            }
        }

        if (!empty($in) && count($in)==2){
            $query = $query->whereIn($in[0],$in[1]);
        }

        return $query->count();
    }

    public function getCV($position=null){
        if (is_null($position)){
            return 0;
        }

        $cv = 0;

        $position->load('deliver');

        if (count($position->deliver)){

            $deliver = $position->deliver->groupBy('resume_id');

            $cv = count($deliver);

        }

        return $cv;

    }

    public function getResultCount($position=null,$result=null){
        if (is_null($position) || is_null($result)){
            return false;
        }

        $name = $result.'count';

        $position->$name = 0;

        $position->load('deliver');

        if (count($position->deliver)){

            foreach ($position->deliver as $item){

                $schedule = DeliverSchedule::where('deliver_id',$item->id)->orderBy('id', 'desc')->first();

                if ($schedule && $schedule->result == $result){

                    $position->$name = $position->$name+1;

                }

            }

        }

    }


}
